/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlets;

import EntityPackage.BusinessLogic;
import EntityPackage.Piatto;
import EntityPackage.Prenotazione;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Marco Berchiatti
 */
public class ElencoPrenotazioni extends HttpServlet {
    @EJB
    private BusinessLogic businessLogic;
    HttpSession session;
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            response.setContentType("text/html");
            session = request.getSession(true);
            int id = (int) session.getAttribute("id");
            Collection<Prenotazione> reservations = businessLogic.findClientById(id).getPrenotazioneCollection();
            String result = "";            
            if (reservations.isEmpty())                
                result = result + "Non sono ancora state effettuate prenotazioni";
            else
                for (Prenotazione iter : reservations) {
                    //result: codicePrenotazione nomeLocation nomeAgenzia elencoMenù data tipologia ora invitati saldo/totale
                    result = result + "Codice prenotazione: " + iter.getIdPrenotazione();
                    String location = iter.getLocation().getIndirizzo();
                    String città = iter.getLocation().getCittà();
                    result = result+"\n"+location + " " +città;
                    String agenzia = iter.getAgenzia().getNome();
                    result = result+"\n"+agenzia;
                    result = result+"\nMenù:";
                    Collection<Piatto> piatti = iter.getMenu().getPiattoCollection();
                    if(piatti.isEmpty())
                        result = result + " elenco dei piatti non disponibile\n";
                    else
                        for(Piatto iter2 : piatti) {
                            result = result +"\t"+ iter2.getDescrizione() +"\n";
                        }
                    String data = iter.getData().toString();
                    int n = data.indexOf(" ");
                    String day = data.substring(0,n);
                    data = data.substring(n+1,data.length());
                    n = data.indexOf(" ");
                    String month = data.substring(0,n);
                    data = data.substring(n+1,data.length());
                    n = data.indexOf(" ");
                    String dayNum = data.substring(0,n);
                    data = data.substring(n+1,data.length());
                    for(int i=0; i<2; i++) {
                        n = data.indexOf(" ");
                        data = data.substring(n+1,data.length());
                    }
                    String year = data;
                    String time = iter.getOra().toString();
                    for(int i=0; i<3; i++) {
                        n = time.indexOf(" ");
                        time = time.substring(n+1,time.length());
                    }
                    n = time.indexOf(" ");
                    time = time.substring(0,n);
                    result = result + day +" "+ dayNum +" "+ month +" "+ year +"\n"+ iter.getTipologia() +", ore "+ time +"\n"+iter.getNumPersone()+" invitati\n"+iter.getSaldo()+" / "+iter.getPrezzo() + " €\n\n";
                }
            request.setAttribute("reservations", result);       
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/mycsm.jsp");
            rd.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
